package com.baizhi.controller;

import com.baizhi.entity.Manager;
import com.baizhi.entity.Triage;
import com.baizhi.service.TriageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("triage")
public class TriageController {
    @Autowired
    private TriageService triageService;

    @RequestMapping("add")
    @ResponseBody
    public Map addTriage(Triage triage){
        System.out.println("triage="+triage);
        Map map = new HashMap();
        try {
            triageService.insertTriage(triage);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("call")
    @ResponseBody
    public Map callTriage(Integer tid, HttpSession session){
        //叫号  就是修改triage  给其修改诊断状态status和医生IDmid
        Triage triage = triageService.selectById(tid);
        triage.setStatus(1);
        //医生从登陆标识中取
        Manager manager = (Manager) session.getAttribute("login");
        triage.setManager(manager);
        System.out.println("叫号后triage="+triage);
        Map map = new HashMap();
        map.put("did",triage.getDepartment().getDid());
        try {
            triageService.updateTriage(triage);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    @ResponseBody
    public Triage getOneTriage(Integer tid){
        Triage triage = triageService.selectById(tid);
        System.out.println("诊断时："+triage);
        return triage;
    }
    @RequestMapping("diagnose")
    @ResponseBody
    public Map diagnoseTriage(Triage triage){
        // 诊断也是是修改triage  给其修改诊断状态status、诊断内容(从页面获取)
        triage.setStatus(2);
        System.out.println("诊断收参："+triage);
        Map map = new HashMap();
        try {
//            map.put("did",triage.getDepartment().getDid());
            System.out.println(11111);
            triageService.updateTriage(triage);
            System.out.println(2222222);
            map.put("did",triage.getDepartment().getDid());
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        System.out.println("诊断后的flag="+map.get("flag"));
        System.out.println("诊断后:"+triage);
        return map;
    }
    @RequestMapping("show")
    @ResponseBody
    public Map showTriage(int page,int limit,int did){
        Map map = new HashMap();
        PageInfo<Triage> pageInfo = triageService.selectByDepartment(did, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
}
